package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.koro.dao.GoodsRepository;
import com.koro.dto.FindGoodsDTO;
import com.koro.dto.SaveGoodsDTO;
import com.koro.entity.Goods;
import com.koro.entity.Users;
import com.koro.mapper.GoodsMapper;
import com.koro.service.GoodsService;
import com.koro.tool.ExcelUtil;
import com.koro.tool.ExcelUtils;
import com.koro.tool.ObjectMapperUtil;
import com.koro.vo.FindGoodsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsRepository goodsRepository;

    @Override
    public Integer save(SaveGoodsDTO dto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(dto,goods);
        if(!"".equals(dto.getId()) || dto.getId() == 0) {
            goodsMapper.insert(goods);
        } else {
            goodsMapper.updateById(goods);
        }
        goodsRepository.save(goods);
        return goods.getId();
    }

    @Override
    public List<FindGoodsVO> findAll(FindGoodsDTO dto) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        if(StringUtils.hasText(dto.getKeywords())) {
            queryWrapper.like("goods_name",dto.getKeywords());
        }
        List<Goods> goods = goodsMapper.selectList(queryWrapper);
        Iterable<Goods> all = goodsRepository.findAll();
        return ObjectMapperUtil.convert(goods, new TypeReference<List<FindGoodsVO>>() {
        });
    }

    @Override
    public FindGoodsVO findById(FindGoodsDTO dto) {
        Goods goods = goodsMapper.selectById(dto.getId());
        FindGoodsVO findGoodsVO = new FindGoodsVO();
        BeanUtils.copyProperties(goods, findGoodsVO);
        return findGoodsVO;
    }

    @Override
    public Integer importExcel(MultipartFile uploadFile) {
        ExcelUtil excelUtil = new ExcelUtil(Goods.class);
        List<Goods> list = null;
        try {
            list = excelUtil.importExcel(uploadFile.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
        list.forEach(g -> {
            System.out.println(g.getGoodsName());
        });
        return null;
    }

    @Override
    public void downLoad(HttpServletResponse response) {
        List<Goods> list = new ArrayList<>();
        for(int i = 0 ; i < 84500 ; i++) {
            Goods goods = new Goods();
            goods.setId(i+1);
            goods.setGoodsName("导出产品" + "-" + i+1);
            goods.setPrice(new BigDecimal(100));
            goods.setStock(10000);
            list.add(goods);
        }

    //    ExcelUtils excelUtils = new ExcelUtils(Goods.class);

        ExcelUtil excelUtil = new ExcelUtil(Goods.class);

        excelUtil.exportExcel(response,list,"导出产品");

    }

    @Override
    public void synGoodsES() {
        goodsRepository.deleteAll();
        List<Goods> goods = goodsMapper.selectList(new QueryWrapper<>());
        goods.forEach(g -> goodsRepository.save(g));
    }
}
